package com.teach.service.impl;

import com.teach.entity.User;
import com.teach.mapper.UserMapper;
import com.teach.service.UserService;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.teach.dto.UserSearch;
import com.teach.mapper.RoleMapper;
import com.teach.util.PasswordUtil;
import com.teach.config.Config;
import com.teach.dto.LoginUser;
import com.teach.dto.UserRegDTO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <pre>
 * TODO : 业务逻辑实现类
 * 系统用户
 * ClassName : com.teach.service.impl.UserServiceImpl
 * Author : J.L.Zhou
 * Tel : 15111047708
 * E-Mail : 12020042@qq.com
 * Date : 2020-05-02 13:24
 * Version : 1.0
 * </pre>
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

	@Autowired
	private RoleMapper roleMapper;
	
	@Autowired
	private Config config;

	@Override
	public QueryWrapper<User> getQueryWrapper(User user) {
		QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
		if(user.getUserId() != null) {
			queryWrapper.eq("USER_ID", user.getUserId());
		}
		if(user.getRoleId() != null) {
			queryWrapper.eq("ROLE_ID", user.getRoleId());
		}
		if(user.getUserName() != null) {
			queryWrapper.eq("USER_NAME", user.getUserName());
		}
		if(user.getUserNickname() != null) {
			queryWrapper.eq("USER_NICKNAME", user.getUserNickname());
		}
		if(user.getUserPassword() != null) {
			queryWrapper.eq("USER_PASSWORD", user.getUserPassword());
		}
		if(user.getUserSex() != null) {
			queryWrapper.eq("USER_SEX", user.getUserSex());
		}
		if(user.getUserBrithdate() != null) {
			queryWrapper.eq("USER_BRITHDATE", user.getUserBrithdate());
		}
		if(user.getUserTel() != null) {
			queryWrapper.eq("USER_TEL", user.getUserTel());
		}
		if(user.getUserAddress() != null) {
			queryWrapper.eq("USER_ADDRESS", user.getUserAddress());
		}
		if(user.getUserImg() != null) {
			queryWrapper.eq("USER_IMG", user.getUserImg());
		}
		if(user.getUserStatus() != null) {
			queryWrapper.eq("USER_STATUS", user.getUserStatus());
		}
		if(user.getUserRegTime() != null) {
			queryWrapper.eq("USER_REG_TIME", user.getUserRegTime());
		}
		return queryWrapper;
	}
	

	@Override
	public LoginUser login(String userName, String userPwd) {
		if (StrUtil.hasBlank(userName, userPwd)) {
			throw new RuntimeException("用户名和密码不能为空");
		}
		
		User u = baseMapper.selectOne(new QueryWrapper<User>().lambda().eq(User::getUserName, userName));//select * from sys_user where user_name=?
		if (u == null) {
			throw new RuntimeException("用户不存在");
		}
		if (!u.getUserPassword().equals(PasswordUtil.encode(userPwd, u.getUserName()))) {
			throw new RuntimeException("密码错误");
		}

		if (u.getUserStatus().equals(1)) {
			throw new RuntimeException("用户被锁定了");
		}

		LoginUser user = new LoginUser();
		BeanUtils.copyProperties(u, user);//user.setUserId(u.getUserId());
		BeanUtils.copyProperties(roleMapper.selectById(u.getRoleId()), user);
		return user;
	}

	@Override
	public QueryWrapper<User> getQueryWrapper(UserSearch search) {
		QueryWrapper<User> qw = new QueryWrapper<User>();
		if (!StrUtil.hasBlank(search.getKey())) {
			qw.and(t -> t.or().like("USER_NICKNAME", search.getKey())
					.or().like("USER_NAME", search.getKey())
					.or().like("USER_TEL", search.getKey())
					.or().like("USER_ADDRESS", search.getKey()));
		}
		if (search.getRoleId() != null) {
			qw.eq("ROLE_ID", search.getRoleId());
		}
		return qw;
	}


	@Override
	public boolean existsUserName(String userName) {
		return baseMapper.selectOne(new QueryWrapper<User>().eq("USER_NAME", userName))!=null;
	}


	@Override
	public User reg(UserRegDTO user) {

		// if(user.getUserName()==null || user.getUserName().trim().equals("")){
		if (StrUtil.hasBlank(user.getUserName())) {
			throw new RuntimeException("用户名不能为空");
		}
		
		if(!user.getUserName().matches("^[A-Za-z]\\w{2,19}$")) {
			throw new RuntimeException("用户名不符合规则");
		}

		if (existsUserName(user.getUserName())) {
			throw new RuntimeException("用户名已经存在");
		}

		// if(user.getUserPassword()==null ||
		// user.getUserPassword().equals("")){
		if (StrUtil.hasEmpty(user.getUserPassword())) {
			throw new RuntimeException("密码不能为空");
		}

		if(!user.getUserPassword().matches("^.{3,20}$")) {
			throw new RuntimeException("密码不符合规范");
		}
	
		String userPassword = PasswordUtil.encode(user.getUserPassword(), user.getUserName());

		User user1 = new User();

		BeanUtil.copyProperties(user, user1);
		user1.setUserPassword(userPassword);

		// 默认值的设置,可配置
		BeanUtil.copyProperties(config.defaultUser, user1);

		try {
			baseMapper.insert(user1);
			return user1;
		} catch (Exception ex) {
			throw new RuntimeException("注册错误",ex);
		}
	}
}
